package com.garlicts.excel.util;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class DateTimeConvertUtil {
	
	private static Map<String, String> map = new HashMap<String, String>();

	public static final String format_1 = "yyyy-MM-dd";
	public static final String format_1_1 = "yyyy-MM-dd HH";
	public static final String format_1_2 = "yyyy-MM-dd HH:mm";
	public static final String format_1_3 = "yyyy-MM-dd HH:mm:ss";
	
	public static final String format_2 = "yyyy/MM/dd";
	public static final String format_2_1 = "yyyy/MM/dd HH";
	public static final String format_2_2 = "yyyy/MM/dd HH:mm";
	public static final String format_2_3 = "yyyy/MM/dd HH:mm:ss";
	
	public static final String format_3 = "yyyy.MM.dd";
	public static final String format_3_1 = "yyyy.MM.dd HH";
	public static final String format_3_2 = "yyyy.MM.dd HH:mm";
	public static final String format_3_3 = "yyyy.MM.dd HH:mm:ss";
	
	public static final String format_4 = "yyyy年MM月dd日";
	public static final String format_4_1 = "yyyy年MM月dd日 HH时";
	public static final String format_4_2 = "yyyy年MM月dd日 HH时mm分";
	public static final String format_4_3 = "yyyy年MM月dd日 HH时mm分ss秒";
	
	public static final String regex_1 = "^\\s*\\d{4}(-){1}\\d{2}(-){1}\\d{2}\\s*";
	public static final String regex_1_1 = "^\\s*\\d{4}(-){1}\\d{2}(-){1}\\d{2}\\s+(\\d{2}){1}\\s*";
	public static final String regex_1_2 = "^\\s*\\d{4}(-){1}\\d{2}(-){1}\\d{2}\\s+(\\d{2}:){1}\\d{2}\\s*";
	public static final String regex_1_3 = "^\\s*\\d{4}(-){1}\\d{2}(-){1}\\d{2}\\s+(\\d{2}:){2}\\d{2}\\s*";
	
	public static final String regex_2 = "^\\s*\\d{4}(/){1}\\d{2}(/){1}\\d{2}\\s*";
	public static final String regex_2_1 = "^\\s*\\d{4}(/){1}\\d{2}(/){1}\\d{2}\\s+(\\d{2}){1}\\s*";
	public static final String regex_2_2 = "^\\s*\\d{4}(/){1}\\d{2}(/){1}\\d{2}\\s+(\\d{2}:){1}\\d{2}\\s*";
	public static final String regex_2_3 = "^\\s*\\d{4}(/){1}\\d{2}(/){1}\\d{2}\\s+(\\d{2}:){2}\\d{2}\\s*";
	
	public static final String regex_3 = "^\\s*\\d{4}(\\.){1}\\d{2}(\\.){1}\\d{2}\\s*";
	public static final String regex_3_1 = "^\\s*\\d{4}(\\.){1}\\d{2}(\\.){1}\\d{2}\\s+(\\d{2}){1}\\s*";
	public static final String regex_3_2 = "^\\s*\\d{4}(\\.){1}\\d{2}(\\.){1}\\d{2}\\s+(\\d{2}:){1}\\d{2}\\s*";
	public static final String regex_3_3 = "^\\s*\\d{4}(\\.){1}\\d{2}(\\.){1}\\d{2}\\s+(\\d{2}:){2}\\d{2}\\s*";
	
	public static final String regex_4 = "^\\s*\\d{4}([\u4e00-\u9fa5]){1}\\d{2}([\u4e00-\u9fa5]){1}\\d{2}([\u4e00-\u9fa5]){1}\\s*";
	public static final String regex_4_1 = "^\\s*\\d{4}([\u4e00-\u9fa5]){1}(\\d{2}([\u4e00-\u9fa5]){1}){2}\\s*(\\d{2}([\u4e00-\u9fa5]){1}){1}\\s*";
	public static final String regex_4_2 = "^\\s*\\d{4}([\u4e00-\u9fa5]){1}(\\d{2}([\u4e00-\u9fa5]){1}){2}\\s*(\\d{2}([\u4e00-\u9fa5]){1}){2}\\s*";
	public static final String regex_4_3 = "^\\s*\\d{4}([\u4e00-\u9fa5]){1}(\\d{2}([\u4e00-\u9fa5]){1}){2}\\s*(\\d{2}([\u4e00-\u9fa5]){1}){3}\\s*";
	
	public static String getFormat(String dateTime) {
		
		if(Pattern.compile(regex_1).matcher(dateTime).matches()) {
			return format_1;
		}else if(Pattern.compile(regex_1_1).matcher(dateTime).matches()) {
			return format_1_1;
		}else if(Pattern.compile(regex_1_2).matcher(dateTime).matches()) {
			return format_1_2;
		}else if(Pattern.compile(regex_1_3).matcher(dateTime).matches()) {
			return format_1_3;
		}else if(Pattern.compile(regex_2).matcher(dateTime).matches()) {
			return format_2;
		}else if(Pattern.compile(regex_2_1).matcher(dateTime).matches()) {
			return format_2_1;
		}else if(Pattern.compile(regex_2_2).matcher(dateTime).matches()) {
			return format_2_2;
		}else if(Pattern.compile(regex_2_3).matcher(dateTime).matches()) {
			return format_2_3;
		}else if(Pattern.compile(regex_3).matcher(dateTime).matches()) {
			return format_3;
		}else if(Pattern.compile(regex_3_1).matcher(dateTime).matches()) {
			return format_3_1;
		}else if(Pattern.compile(regex_3_2).matcher(dateTime).matches()) {
			return format_3_2;
		}else if(Pattern.compile(regex_3_3).matcher(dateTime).matches()) {
			return format_3_3;
		}else if(Pattern.compile(regex_4).matcher(dateTime).matches()) {
			return regex_4;
		}else if(Pattern.compile(regex_4_1).matcher(dateTime).matches()) {
			return regex_4_1;
		}else if(Pattern.compile(regex_4_2).matcher(dateTime).matches()) {
			return regex_4_2;
		}else if(Pattern.compile(regex_4_3).matcher(dateTime).matches()) {
			return regex_4_3;
		}
		
		return null;
		
	}
	
	public static void main(String[] args) {
		
		String[] strs = new String[] {"2018-11-02", "2018-11-02 12", "2018-11-02 12:45" , "2018-11-02 12:45:00", 
				"2018/11/02", "2018/11/02 12", "2018/11/02 12:45" , "2018/11/02 12:45:00", 
				"2018.11.02", "2018.11.02 12", "2018.11.02 12:45" , "2018.11.02 12:45:00",
				"2018年11月02日", "2018年11月02日 12时", "2018年11月02日 12时45分" , "2018年11月02日 12时45分00秒"};
		for(int i=0; i<strs.length; i++) {
			String format = getFormat(strs[i]);
			System.out.println(strs[i]+" == " + format);
		}
		
	}
	
	
	
}
